package xyz.cerberu.cet.user.controller;

import xyz.cerberu.cet.global.annotation.JsonFilter;
import xyz.cerberu.cet.global.domain.Global;
import xyz.cerberu.cet.global.domain.Page;
import xyz.cerberu.cet.global.domain.Result;
import xyz.cerberu.cet.global.utils.ListUtils;
import xyz.cerberu.cet.global.utils.ResultUtils;
import xyz.cerberu.cet.user.domain.Comment;
import xyz.cerberu.cet.user.domain.User;
import xyz.cerberu.cet.user.exception.CommentException;
import xyz.cerberu.cet.user.service.CommentServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author β世界
 * Created on 9:29
 * @Description 评论的控制器
 */
@RequestMapping("/comment")
@Controller
public class CommentControl {
    private final CommentServiceImpl commentServiceImpl;
    private final Global global;

    @Autowired
    public CommentControl(CommentServiceImpl commentServiceImpl, Global global) {
        this.commentServiceImpl = commentServiceImpl;
        this.global = global;
    }

    /**
     * @author β世界 on 11:03 2020/9/18
     * 用户评论接口(只允许登陆后的用户来评论)
     * @param commentContent: 评论内容
     * @param aniId: 动漫id
     * @return void
     **/
    @ResponseBody
    @RequiresUser
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result<Object> addComment(String commentContent,String aniId) {
        // 获取用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        // 设置实体
        Comment comment = new Comment();
        comment.setAniId(aniId);
        comment.setCommentContent(commentContent);
        comment.setUser(user);

        return submitComment(comment);
    }

    /**
     * @author β世界 on 23:33 2020/10/13
     * 用于回复评论，我们需要记录他的上级回复的评论id
     * @param commentContent: 评论的内容
     * @param replyComment: 被回复的评论
     * @param aniId: 动漫id
     * @return com.Cet.common.domain.Result
     **/
    @RequiresUser
    @ResponseBody
    @RequestMapping(value = "/replyComment",method = RequestMethod.POST)
    public Result<Object> replyComment(String commentContent, int replyComment, String aniId){
        // 获取用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();

        // 设置实体
        Comment comment = new Comment();
        comment.setAniId(aniId);
        comment.setCommentContent(commentContent);
        comment.setUser(user);
        comment.setSuperior(replyComment);
        // 提交实体
        return submitComment(comment);

    }


    /**
     * @author β世界 on 23:34 2020/10/13
     * 用于获取一个动漫信息的评论信息
     * @param anime: 动漫的ID
     * @param page: 当前页码
     * @return com.Cet.common.domain.Result
     **/
    @ResponseBody
    @RequestMapping(value = "/getComment",method = RequestMethod.GET)
    @JsonFilter(type = Comment.class,filter = "aniId,isShow")
    public Result<Object> getAnimeComment(String anime,int page){
        // 获取一个动漫的所有的评论，并且进行分页处理
        List<Comment> comments = commentServiceImpl.getAnimeComment(anime,page);
        // 进行评论回复的分划
        ListUtils.clearReplyComment(comments);
        // 对内容进行分页处理
        Page<Comment> commentPages = Page.build(comments,page, global.getCommentShowNumber());

        return ResultUtils.genSuccessResult(commentPages);
    }

    private Result<Object> submitComment(Comment comment){
        try{
            commentServiceImpl.addComment(comment);
            return ResultUtils.genSuccessResult("评论成功");
        }catch (Exception e){
            e.printStackTrace();
            // 如果这个属于评论异常就抛出这个异常信息
            if(e instanceof CommentException){
                return ResultUtils.genFailResult(e.getMessage());
            }
            return ResultUtils.genFailResult("评论失败");
        }
    }

}
